﻿using Microsoft.Maui.Controls;
using System.Runtime.Versioning;

namespace Brilliant_see.UI.Behaviors;

public class HideStatusBarBehavior : Behavior<Page>
{
    protected override void OnAttachedTo(Page page)
    {
        base.OnAttachedTo(page);
        SetStatusBarVisibility(false);
        page.Disappearing += (s, e) => SetStatusBarVisibility(true);
    }

    private static void SetStatusBarVisibility(bool isVisible)
    {
#if ANDROID
        var activity = Platform.CurrentActivity ?? throw new InvalidOperationException("Android Activity can't be null.");
        var window = activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
#endif
        if (isVisible)
        {
#if ANDROID
            window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
            window.ClearFlags(Android.Views.WindowManagerFlags.Fullscreen);
#endif
        }
        else
        {
#if ANDROID
            window.AddFlags(Android.Views.WindowManagerFlags.Fullscreen);
            window.AddFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
#endif
        }
    }
}